﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace CrashSimul8or
{
    class Nurse : Unit
    {
        private bool notMoving;
        private bool curingPatient;
        List<Patient> patients;
        Patient targetPatient;
        private float mtime;

        public Nurse(float X, float Y, List<Texture2D> particleTextures, List<Patient> patients, int sT)
            : base(X, Y, particleTextures, sT)
        {
            texture = ResourceManager.getTexture("Nurse");

            this.patients = patients;
            notMoving = true;
            curingPatient = false;
            mtime = 0;
            targetPatient = null;
        }

        public override void Update(GameTime gameTime)
        {
            if (createdTime == 0)
            {
                spawnTime = gameTime.TotalGameTime.Seconds + spawnTime;
                createdTime = gameTime.TotalGameTime.Seconds;
            }

            int timeToSpawn = spawnTime - gameTime.TotalGameTime.Seconds;

            if (timeToSpawn <= 0 && !spawned)
            {
                spawned = true;
                this.goTo(UnitDest.X, UnitDest.Y);
            }

            if (!spawned)
            {
                status = timeToSpawn.ToString();
            }
            else
            {
                float distance;
                Vector2.Distance(ref UnitDest, ref UnitLoc, out distance);
                if (distance < 2)
                {
                    notMoving = true;
                    status = "Ready";
                    UnitLoc = new Vector2(UnitDest.X, UnitDest.Y);

                    if (!curingPatient && targetPatient == null)
                    {
                        findPatient();
                    }
                    else if (notMoving && targetPatient!=null)
                    {
                        curePatient(gameTime);
                        status = "Curing";
                    }
                }
                else
                {
                    UnitLoc += (float)1.5 * (new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle)));
                    status = "Moving";
                }
                glowParticle.Update(UnitLoc);
            }
        }

        private void findPatient()
        {
            foreach (Patient p in patients)
            {
                Vector2 diff = p.UnitLoc - UnitLoc;
                if (diff.Length() < 80 && p.isMarked() && !p.dead && !p.cured)
                {
                    curingPatient = true;
                    targetPatient = p;
                }
            }
        }

        private void curePatient(GameTime gameTime)
        {
            //Console.WriteLine("Hjúkka að heala: " + (gameTime.TotalGameTime.Seconds - mtime));
            if (targetPatient.dead || targetPatient.cured)
            {
                curingPatient = false;
                targetPatient = null;
                mtime = 0;
            }
            else
            {
                if (mtime == 0)
                {
                    mtime = gameTime.TotalGameTime.Seconds;
                }
                else if (gameTime.TotalGameTime.Seconds - mtime > 10) //10 sec að lækna
                {
                    targetPatient.curePatient();
                    curingPatient = false;
                    mtime = 0;
                    targetPatient = null;
                }
            }
        }

        public override void goTo(float X, float Y)
        {
            base.goTo(X, Y);
            notMoving = false;
            curingPatient = false;
            mtime = 0;
            targetPatient = null;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
            if (curingPatient)
            {
                spriteBatch.Draw(ResourceManager.getTexture("ATH"),
                    new Rectangle(
                        (int)(UnitLoc.X - unitSize / 4),
                        (int)(UnitLoc.Y) - unitSize,
                        32,
                        32),
                    Color.White);
            }
        }
    }
}
